package com.hb.trade.api.push.task.winbidbulletinphase;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.hb.core.pojo.center.au.dto.OrgDTO;
import com.hb.core.pojo.project.calibration.dto.BidWinResultDTO;
import com.hb.core.pojo.project.calibration.dto.BidWinResultSupplyDTO;
import com.hb.trade.api.push.task.wincandidatebulletinphase.Price;
import com.hb.trade.client.center.OrgClient;
import lombok.Data;
import org.fzzn.core.model.msg.Message;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * describe:
 *
 * @author zhongdl
 * @date 2022/02/24
 */
@Data
public class SectionsBidder {

    /**
     * 标段（包）编号
     */
    @JsonProperty("BID_SECTION_CODE")
    private String bidSectionCode;
    /**
     * 标段（包）编号
     */
    @JsonProperty("BIDDERS")
    private BidderS bidderS;


    public SectionsBidder(String unions,String bidSectionCode,BidWinResultSupplyDTO bidWinResultSupply) {
        BidderS bidderS = new BidderS();
        List<Bidder> bidderList = CollUtil.list(false);

        Bidder bidder = new Bidder();
        // 手动添加的
        if(null == bidWinResultSupply.getOrgId()){
            bidder.setBidderCodeType("05");
            bidder.setBidderCode(bidWinResultSupply.getCreditCode());
            bidder.setBidderName(bidWinResultSupply.getOrgName());
            if (unions.equals("1")){
                bidder.setUnions(unions);
            }
            Price price = new Price(bidWinResultSupply.getSuccessfulAmount(),"156");
            Map<String, Price> priceMap = MapUtil.newHashMap(1);
            priceMap.put("PRICE",price);
            bidder.setPrice(priceMap);
        }else{
            OrgClient orgClient = SpringUtil.getBean(OrgClient.class);
            Message<OrgDTO> org = orgClient.getOrgById(bidWinResultSupply.getOrgId());
            if (org.isFail() || org.getData() == null){
                return;
            }
            OrgDTO orgDTO = org.getData();

            bidder.setBidderCodeType("05");
            bidder.setBidderCode(orgDTO.getCreditCode());
            bidder.setBidderName(orgDTO.getOrgName());
            if (unions.equals("1")){
                bidder.setUnions(unions);
            }
            Price price = new Price(bidWinResultSupply.getSuccessfulAmount(),"156");
            Map<String, Price> priceMap = MapUtil.newHashMap(1);
            priceMap.put("PRICE",price);
            bidder.setPrice(priceMap);
        }

        this.bidSectionCode = bidSectionCode;
        bidderList.add(bidder);
        bidderS.setBidderList(bidderList);
        setBidderS(bidderS);
    }

}
